<?php

namespace common\widgets;

use yii\grid\ActionColumn;
use Yii;
use yii\helpers\ArrayHelper;

class DropdownActionColumn extends ActionColumn
{
    public $items = [];

    /**
     * {@inheritdoc}
     */
    protected function initDefaultButtons()
    {
        if ( ! isset($this->buttons['view']))
        {
            $this->buttons['view'] = function ($url, $model, $key)
            {
                $options = array_merge([
                    'title'      => Yii::t('yii', 'View'),
                    'aria-label' => Yii::t('yii', 'View'),
                    'data-pjax'  => '0',
                ], $this->buttonOptions);
                return [
                    'label' => Yii::t('yii', 'View'),
                    'url'   => $url,
                ];
            };
        }
        if ( ! isset($this->buttons['update']))
        {
            $this->buttons['update'] = function ($url, $model, $key)
            {
                $options = array_merge([
                    'title'      => Yii::t('yii', 'Update'),
                    'aria-label' => Yii::t('yii', 'Update'),
                    'data-pjax'  => '0',
                ], $this->buttonOptions);
                return [
                    'label' => Yii::t('yii', 'Update'),
                    'url'   => $url,
                ];
            };
        }
        if ( ! isset($this->buttons['delete']))
        {
            $this->buttons['delete'] = function ($url, $model, $key)
            {
                $options = array_merge([
                    'title'        => Yii::t('yii', 'Delete'),
                    'aria-label'   => Yii::t('yii', 'Delete'),
                    'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
                    'data-method'  => 'post',
                    'data-pjax'    => '0',
                ], $this->buttonOptions);
                return [
//                    'label'=>Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, $options)
                    'label' => Yii::t('yii', 'Delete'),
                    'url'   => $url,

                ];
            };
        }
    }

    /**
     * @inheritdoc
     */
    protected function renderDataCellContent($model, $key, $index)
    {
        $definedItems = [];
        preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) use ($model, $key, $index, &$definedItems)
        {
            $name = $matches[1];
            if (isset($this->buttons[$name]))
            {
                $url = $this->createUrl($name, $model, $key, $index);
                $item = call_user_func($this->buttons[$name], $url, $model, $key);

                $definedItems[] = $item;
            }
            else
            {
                return '';
            }
        }, $this->template);
        $dropDownId = 'dda' . crc32($model->className()) . $index;
        $dropDown = Dropdown::widget([
            'items'          => ArrayHelper::merge($this->items, $definedItems),
            'registerPlugin' => false
            //'options' => ['aria-labelledby'=>$dropDownId],
        ]);
        $html = <<<EOF
<div class="dropdown">
     <a data-target="#" href="#" data-toggle="dropdown" class="dropdown-toggle" >操作 <b class="caret"></b></a>
     {$dropDown}
 </div>
EOF;
        return $html;
    }
}
